package com.example.helloffmpeg.util

import android.content.Context
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

object AssertUtil {

    @JvmStatic
    fun copyFile(context: Context): File? {
        val fileName = "sintel.mp4"
        var inputStream: InputStream? = null
        var fileOutputStream: FileOutputStream? = null
        return try {
            //将asset中的文件复制到sd卡中
            inputStream = context.assets.open(fileName)
            val file: File = File(context.cacheDir.absolutePath, fileName)
            if (file.exists()) {
                file.delete()
            }
            fileOutputStream = FileOutputStream(file)
            val buffer = ByteArray(8192)
            var byteCount: Int
            while (inputStream.read(buffer).also { byteCount = it } != -1) {
                fileOutputStream.write(buffer, 0, byteCount)
            }
            fileOutputStream.flush()
            file
        } catch (e: Exception) {
            LogU.d(e.toString())
            null
        } finally {
            try {
                inputStream?.close()
                fileOutputStream?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

}